package com.ai.controller;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: springboot3-demos
 * @description:
 * @author: Reagan Li
 * @create: 2024-04-25 16:26
 **/

@RestController
@RequestMapping("/ai")
public class SimpleAiController {

    private final ChatClient chatClient;

    public SimpleAiController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @GetMapping("/chat")
    public Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", chatClient.call(message));
    }

    @GetMapping("/stream/chat")
    public String streamChat(@RequestParam("message") String message) {
        PromptTemplate promptTemplate = new PromptTemplate("你是一个高级运营，你擅长于写各种运营文案，根据：{message} 场景写一个不少于100字的文案");
        Prompt prompt = promptTemplate.create(Map.of("message", message));
        ChatResponse call = chatClient.call(prompt);
        return call.getResult().toString();
    }
}
